package com.iweb.dao;

import com.common.model.Page;
import com.iweb.pojo.Admin;
import com.iweb.pojo.User;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {

    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    public AdminDaoImpl(Connection conn) {
        this.conn = conn;
    }


    @Override
    public void save(Admin admin) throws Exception {

        String sql = "insert into tb_admin (aid,username,password) values (?,?,?)";

        pst = conn.prepareStatement(sql);
        pst.setString(1,admin.getAid());
        pst.setString(2,admin.getUsername());
        pst.setString(3,admin.getPassword());
        pst.executeUpdate();
    }

    @Override
    public void deleteById(Serializable id) throws Exception {
        String sql = "delete from tb_admin where aid = ?";
        pst = conn.prepareStatement(sql);
        pst.setObject(1,id);
        pst.executeUpdate();
    }

    @Override
    public void updateById(Admin admin) throws Exception {

    }


    @Override
    public List<User> findAll() throws Exception {
        return null;
    }

    @Override
    public Admin findById(Serializable id) throws Exception {
        Admin admin = null;
        String sql = "select * from tb_admin where aid = ?";
        pst = conn.prepareStatement(sql);
        pst.setObject(1,id);

        rs = pst.executeQuery();

        while (rs.next()){
            admin = createInstance(rs);
        }

        return admin;
    }

    /**
     * 如果返回 null 说明没有用户
     * @param username
     * @return
     * @throws Exception
     */
    @Override
    public Admin findUserByUsername(String username) throws Exception {
        Admin admin = null;
        String sql = "select * from tb_admin where username = ?";
        pst=conn.prepareStatement(sql);
        pst.setObject(1,username);
        rs = pst.executeQuery();

        while (rs.next()){
            admin = createInstance(rs);
        }
        return admin;
    }

    @Override
    public long selectCount() throws Exception {
        String sql = "select count(1) from tb_admin";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        rs.next();
        return rs.getInt(1);
    }

    @Override
    public List<Admin> findAll(Page page) throws Exception {
        List<Admin> list = new ArrayList<>();
        String sql = "SELECT * FROM tb_admin LIMIT ?,?";
        pst = conn.prepareCall(sql);

        pst.setInt(1,(page.getPage()-1)*page.getLimit());
        pst.setInt(2,page.getLimit());

        rs = pst.executeQuery();
        Admin admin = null;
        while (rs.next()){
            admin = createInstance(rs);
            list.add(admin);
        }

        return list;
    }


    private Admin createInstance(ResultSet rs)throws Exception{
        Admin admin = new Admin();
        admin.setAid(rs.getString("aid"));
        admin.setUsername(rs.getString("username"));
        admin.setPassword(rs.getString("password"));
        admin.setPhone(rs.getString("phone"));
        return admin;
    }
}
